﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HttpFileServer
{
    public class FullFileResponse : FemtoHttpd.FemtoHttpResponse
    {
        private readonly FileInfo fi;
        private FullFileResponse(Stream output, FileInfo fi) : base(output)
        {
            this.fi = fi;
            ContentType = MiMeType.GetMiMeType(fi.Extension);
            Header["Content-Length"] = $"{fi.Length}";

            //if(Util.IsVideo(fi.Name))
           // Header["Content-Disposition"] = "inline";
        }
        public static FullFileResponse Create(Stream output, string fileName)
        {
            var fi = new FileInfo(fileName);
            return fi.Exists ? new FullFileResponse(output, fi) : null;
        }
        protected override void WriteContent()
        {
            using (var fs = fi.OpenRead())
            {
                byte[] buff = new byte[1024 * 100];
                int len;
                while ((len = fs.Read(buff, 0, buff.Length)) > 0)
                {
                    Output.Write(buff, 0, len);
                }
            }
        }
    }
}
